import { createRouter, createWebHistory  } from "vue-router";

const routes = [
    {
      path: "/",
      name: "Login",
      component: () => import('../pages/Login.vue'),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import('../pages/Login.vue'),
  },
  {
    path: "/main",
    name: "Main",
    component: () => import('../pages/Main.vue'),
    children: [
      {
        path: "/cotegory/list", // 去掉前面的斜杠，使用相对路径
        name: "CategoryList",
        component: () => import("../components/main/CategoryList.vue"),
      },
      {
        path: "/product/list", // 去掉前面的斜杠
        name: "NewsTypeList",
        component: () => import("../components/main/ProductList.vue"),
      },
       {
        path: "/product/edit/:id?", // 去掉前面的斜杠
        name: "ProductEdit",
        component: () => import("../components/main/ProductEdit.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: routes,
});



// 全局前置守卫（优化实现）
// router.beforeEach((to, from, next) => {
//   const userStore = useUserStore();
//   if (to.name === "Login") {
//     next();
//   } else {
//     // 检查登录状态（处理store未初始化场景）
//     if (!userStore.isLogin) {
//       //ElMessage.error("无权访问，请先登录！");
//       next({ name: "Login" }); // 使用name跳转，避免路径变更问题
//     } else {
//       next();
//     }
//   }
// });

export default router; //暴露给外部使用